package com.icangya.common.config;

import com.icangya.Value.FootBall;
import com.icangya.domain.AdvertRecord;
import com.icangya.service.IAdvertService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;

import java.util.List;
import java.util.Random;

//@Component
//@Order(value=499)
public class Pageviews implements CommandLineRunner {


    private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IAdvertService iAdvertService;

      @Override
      public void run(String... args) throws Exception {

        logger.info(">>>>>>>>>>>>>>>服务启动后执行广告浏览量虚假递增 <<<<<<<<<<<<<");

        new Thread(new Runnable() {

            private boolean flag = true;
            @Override
            public void run() {
               while (flag){
                        //等于 0 就是关闭浏览量虚假自动增加
//                   logger.info("*************"+FootBall.ADVERT_SHAMPAGEVIEW+"***********");
                   if (FootBall.ADVERT_SHAMPAGEVIEW == 1){
                        try {
//                            logger.info("*************获取指定广告***********");
                            List<AdvertRecord> advertRecordList =  iAdvertService.getAdvertRecordShame();
//                            logger.info("*************获取指定广告数量"+advertRecordList.size()+"***********");
                            // 每隔3秒 将指定广告数组的get(i)增加浏览量
                            for (int i =0;i<advertRecordList.size();i++){
                                Thread.sleep(1000);
                                int ramd = new Random().nextInt(20) +1;
//                                logger.info("此次浏览量虚假添加的随机整数为："+ramd + "广告ID：" + advertRecordList.get(i).getId());
                                iAdvertService.updateAdvertShampageview(advertRecordList.get(i).getId(),advertRecordList.get(i).getShamPageView()+ramd);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }


            public void setFlagTofalse(){
                flag = false;
            }
        }).start();

    }

}